package wvtool;

import edu.udo.cs.wvtool.generic.tokenizer.WVTTokenizer;
import edu.udo.cs.wvtool.main.WVTDocumentInfo;
import edu.udo.cs.wvtool.util.TokenEnumeration;
import edu.udo.cs.wvtool.util.WVToolException;
import org.lionsoul.jcseg.ASegment;
import org.lionsoul.jcseg.core.*;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者: LDL
 * 功能说明:
 * 创建日期: 2015/6/17 17:13
 */
public class ChineseTokenizer implements WVTTokenizer,TokenEnumeration {

    private final List currentTokens;


    private TokenEnumeration input;

    private final WVTTokenizer tokenizer;

    private static JcsegTaskConfig config = new JcsegTaskConfig();
    private static ADictionary dic = DictionaryFactory
            .createDefaultDictionary(config);
    private static ASegment seg;
    static {
        seg = null;
        try {
            seg = (ASegment) SegmentFactory
                    .createJcseg(JcsegTaskConfig.SIMPLE_MODE,
                            new Object[]{config, dic});
        } catch (JcsegException e) {
            e.printStackTrace();
        }
    }

    public ChineseTokenizer(WVTTokenizer tokenizer) {

        this.tokenizer = tokenizer;
        input = null;
        currentTokens = new ArrayList();
    }

    @Override
    public String nextToken() throws WVToolException {
        String result = null;

        // If unequal null, return the current token and read another one from
        // the stream
        if (currentTokens.size() > 0) {
            result = (String) currentTokens.get(0);
            currentTokens.remove(0);
            if (currentTokens.size() == 0)
                readNextToken();
        } else
            result = null;

        return result;
    }

    @Override
    public boolean hasMoreTokens() {
        if (input != null)
            return (currentTokens.size() > 0);
        else
            return false;
    }

    @Override
    public TokenEnumeration tokenize(Reader reader, WVTDocumentInfo wvtDocumentInfo) throws WVToolException {
        if (reader != null) {

            input = tokenizer.tokenize(reader, wvtDocumentInfo);
            readNextToken();
            return this;

        } else
            return null;
    }

    private void readNextToken() throws WVToolException {

        if (input.hasMoreTokens()) {

            String token = input.nextToken();
            IWord word;
            try {
                seg.reset(new StringReader(token));
                while ( (word = seg.next()) != null) {
                    currentTokens.add(word.getValue());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
